﻿using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace MyWeb.Common
{
    public class DataBindSQL
    {
        public DataBindSQL()
        {
        }
        //Hàm thực thi và trả về DataTable
        public DataTable TableThongKe()
        {
            DataTable dtb = new DataTable();
            //string ConnectionString = @"Server =PC2013010908BYP\XUANHIEP;Initial Catalog=ADRI_VN;Persist Security Info=True;User ID=sa;Password=123456;";
            SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
            try
            {
                // Mở kết nối
                Conn.Open();
                DataSet ds = new DataSet();
                SqlCommand cmdUpd = new SqlCommand("spThongKe_Edit", Conn);
                cmdUpd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmdUpd);
                // Đổ dữ liệu vào DataSet
                da.Fill(ds);
                dtb = ds.Tables[0];
            }
            catch
            {
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
                Conn.Dispose();
            }
            return dtb;
        }

        public static string ExecuteScalar(string cmd)
        {
            string strReturn = "";
            using (SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString))
            {
                using (SqlCommand dbCmd = new SqlCommand(cmd, dbConn))
                {
                    dbCmd.CommandType = CommandType.Text;
                    dbConn.Open();
                    strReturn = dbCmd.ExecuteScalar().ToString();
                    dbConn.Close();
                }
            }
            return strReturn;
        }
    }
}
